package com.mvc.service;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import com.mvc.entity.ActionInfo;

public class MethodMapping {

	private static HashMap<String,String> methodMap = new HashMap<String,String>();
	
	static{
		methodMap.put("login.login", "login");
		methodMap.put("register.abc", "regist");
	}
	
	public static String getMethod(String key) {
		return methodMap.get(key);
	}
	
	public static Object execute(String className,String methodName,ActionInfo actionInfo) {
		Object result = null;
		MethodService methodService = null;
		try {
			Class clazz = Class.forName(className);
			methodService = (MethodService)clazz.newInstance();
			Method[] methodArr = clazz.getMethods();
			for (Method method : methodArr) {
				if(method.getName().equals(methodName)) {
					result = method.invoke(clazz.newInstance(), actionInfo);
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
}
